import { ResponseData, SUCCESS_CODE } from '@/utils/request';
import { request } from 'umi';
import {
  AddRoleTemplateBean,
  AddTenantRoleBean,
  UpdateRoleNameBean,
  UpdateRolePermissionBean,
} from './data';
import { message } from 'antd';
import { RequestData } from '@ant-design/pro-table';

// 获取客户角色列表
export async function getTenantRoleList(): Promise<ResponseData<any>> {
  const res = await request('/role/query_tenant_role');
  if (res) return { code: res.code, data: res.data, success: true, message: '' };
  return res;
}

// 获取角色模板列表
export async function getRoleTemplateList(): Promise<ResponseData<any>> {
  const res = await request('/role/query_template');
  if (res) return { code: res.code, data: res.data, success: true, message: '' };
  return res;
}

// 分页查询角色
export async function pageQueryRole(params: {
  name?: string;
  level_code?: number;
  position_id?: string;
  page?: number;
  pageSize?: number;
}): Promise<RequestData<any>> {
  const res = await request('/role/query', {
    params: {
      page: 1,
      pageSize: 10,
      ...params,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }
  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}

/**
 * 添加模板角色
 * @param data
 * @returns
 */
export async function addRoleTemplate(data: AddRoleTemplateBean): Promise<ResponseData<any>> {
  const res = await request('/role/add/template', {
    method: 'POST',
    data: data,
  });
  if (res) return { code: res.code, data: res.data, success: true, message: '' };
  return res;
}

/**
 * 添加客户角色
 * @param data
 * @returns
 */
export async function addTenantRole(data: AddTenantRoleBean): Promise<ResponseData<any>> {
  const res = await request('/role/add/tenant', {
    method: 'POST',
    data: data,
  });
  if (res) return { code: res.code, data: res.data, success: true, message: '' };
  return res;
}

/**
 * 更新角色名字
 * @param data
 * @returns
 */
export async function updateRoleName(data: UpdateRoleNameBean): Promise<ResponseData<any>> {
  const res = await request('/role/update', {
    method: 'POST',
    data: data,
  });
  if (res) return { code: res.code, data: res.data, success: true, message: '' };
  return res;
}

/**
 * 更新角色权限
 * @param data
 * @returns
 */
export async function updateRolePermission(
  data: UpdateRolePermissionBean,
): Promise<ResponseData<any>> {
  const res = await request('/role/permission/update', {
    method: 'POST',
    data: data,
  });
  if (res) return { code: res.code, data: res.data, success: true, message: '' };
  return res;
}

/**
 * 查看角色权限
 * @param params
 * @returns
 */
export async function queryAdminRoleAccessList(params: {
  role_id: string;
  page?: number;
  pageSize?: number;
}): Promise<RequestData<any>> {
  const res = await request('/role/permission/view', {
    params: {
      page: 1,
      pageSize: 1000,
      ...params,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }
  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}

/**
 * 查看可选权限
 * @param params
 * @returns
 */
export async function fetchConfigurableAuthPermission(params: {
  role_id: string;
  page?: number;
  pageSize?: number;
}): Promise<RequestData<any>> {
  const res = await request('/role/permission/view_v2', {
    params: {
      page: 1,
      pageSize: 1000,
      ...params,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }
  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}

/**
 * 查看当前所有可选权限 包括已存在
 * @param params
 * @returns
 */
export async function fetchConfigurableAuthPermissionV2(params: {
  role_id: string;
  page?: number;
  pageSize?: number;
}): Promise<RequestData<any>> {
  const res = await request('/role/permission/view_v3', {
    params: {
      page: 1,
      pageSize: 1000,
      ...params,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }
  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}

/**
 * 删除当前所有可选权限 包括已存在
 * @param params
 * @returns
 */
export async function deleteAuthPermissionV2(params: { id: string }): Promise<RequestData<any>> {
  const res = await request('/role/delete', {
    data: {
      page: 1,
      pageSize: 1000,
      ...params,
    },
    method: 'POST',
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error(res.message);
    return { success: false, total: 0, data: [] };
  }
  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}
